package com.wutuo.aggragation.rollcall.dao;

import com.wutuo.aggragation.rollcall.domain.Task;
import org.apache.ibatis.jdbc.SqlBuilder;

import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class TaskSqlProvider {

    public String insertSelective(Task record) {
        BEGIN();
        INSERT_INTO("task");

        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }

        if (record.getTaskName() != null) {
            VALUES("task_name", "#{taskName,jdbcType=VARCHAR}");
        }

        if (record.getTaskTime() != null) {
            VALUES("task_time", "#{taskTime,jdbcType=TIMESTAMP}");
        }

        if (record.getTaskType() != null) {
            VALUES("task_type", "#{taskType,jdbcType=VARCHAR}");
        }

        if (record.getCreateBy() != null) {
            VALUES("create_by", "#{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            VALUES("create_date", "#{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            VALUES("update_by", "#{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            VALUES("update_date", "#{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            VALUES("remarks", "#{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            VALUES("del_flag", "#{delFlag,jdbcType=CHAR}");
        }
        if (record.getInstitutionId() != null) {
            VALUES("institution_id", "#{institutionId,jdbcType=CHAR}");
        }
        return SQL();
    }

    public String updateByPrimaryKeySelective(Task record) {
        BEGIN();
        UPDATE("task");

        if (record.getTaskName() != null) {
            SET("task_name = #{taskName,jdbcType=VARCHAR}");
        }

        if (record.getTaskTime() != null) {
            SET("task_time = #{taskTime,jdbcType=TIMESTAMP}");
        }

        if (record.getCreateBy() != null) {
            SET("create_by = #{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            SET("create_date = #{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            SET("update_by = #{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            SET("update_date = #{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            SET("remarks = #{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            SET("del_flag = #{delFlag,jdbcType=CHAR}");
        }
        if (record.getInstitutionId() != null) {
            SET("institution_id = #{institutionId,jdbcType=CHAR}");
        }

        if (record.getTaskType() != null) {
            SET("task_type = #{taskType,jdbcType=VARCHAR}");
        }
        WHERE("id = #{id,jdbcType=BIGINT}");

        return SQL();
    }

    public String selectByObject(Task record) {
        BEGIN();
        SqlBuilder.SELECT("*");
        SqlBuilder.FROM("task");

        if (record.getTaskName() != null) {
            WHERE("task_name = #{taskName,jdbcType=VARCHAR}");
        }

        if (record.getTaskTime() != null) {
            WHERE("task_time = #{taskTime,jdbcType=TIMESTAMP}");
        }

        if (record.getTaskType() != null) {
            WHERE("task_type = #{taskType,jdbcType=VARCHAR}");
        }

        if (record.getCreateBy() != null) {
            WHERE("create_by = #{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            WHERE("create_date = #{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            WHERE("update_by = #{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            WHERE("update_date = #{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            WHERE("remarks = #{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            WHERE("del_flag = #{delFlag,jdbcType=CHAR}");
        }

        if (record.getId() != null) {
            WHERE("id = #{id,jdbcType=BIGINT}");
        }
        if (record.getInstitutionId() != null) {
            VALUES("institution_id", "#{institutionId,jdbcType=CHAR}");
        }
        return SQL();
    }

    public String selectByInstitutionId(Map<String, Object> param) {
        BEGIN();
        SqlBuilder.SELECT("*");
        SqlBuilder.FROM("task");

        if (param.get("institutionId") != null) {
            WHERE("institution_id = #{institutionId,jdbcType=BIGINT}");
        }

        SqlBuilder.ORDER_BY("create_date desc");

        StringBuffer sbf = new StringBuffer(SQL());
        if (param.get("index") != null) {
            sbf.append(" limit #{index,jdbcType=INTEGER}, #{size,jdbcType=INTEGER}");
        }

        return sbf.toString();
    }

}
